package com.cheyun.netsalev3.viewmodel

import android.widget.Switch
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.bean.Notice
import com.cheyun.netsalev3.repository.NewsSetRepository
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.google.gson.Gson

class NewsSetActivityViewModel(var newsSetRepository: NewsSetRepository) : BaseViewModel() {
    var notice = MutableLiveData<Notice>()

    init {
        MySharedPreferences.getNotice()?.apply {
            notice.postValue(this)
        }
    }
    fun onClickView(view: Switch, pos: String) {
        var notice = notice.value ?: Notice("1", "1", "1", "1", "1")
        when (pos) {
            "system" -> {
                if (view.isChecked){
                    notice.system="1"
                    notice.group="1"
                    notice.receive="1"
                    notice.change="1"
                    notice.share="1"
                }else{
                    notice.system="0"
                    notice.group="0"
                    notice.receive="0"
                    notice.change="0"
                    notice.share="0"
                }

            }
            "group" -> notice.group = if (view.isChecked) "1" else "0"
            "receive" -> notice.receive = if (view.isChecked) "1" else "0"
            "change" -> notice.change = if (view.isChecked) "1" else "0"
            "share" -> notice.share = if (view.isChecked) "1" else "0"
        }
        var json = Gson().toJson(notice)
        var map = Gson().fromJson(json, HashMap<String, String>()::class.java)
        newsSetRepository.setNotice(map,this::setDate)
    }
    fun setDate(item :Notice){
        notice.postValue(item)
    }

}